package com.fasteat.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasteat.message.service.MsmService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author 白sa能
 * @className
 * @date Created in 10:20 AM 1/14/21
 * @package com.fasteat.message.service.impl
 * @Description
 */
@Service
public class MsmServiceImpl implements MsmService {

    @Value("${aliyun.accessKeyID}")
    private String AccessKey;

    @Value("${aliyun.accessSecret}")
    private String AccessSecret;

    @Value("${aliyun.signName}")
    private String SignName;

    @Value("${aliyun.changeMessageTemplate}")
    private String ChangeMessageTemplate;

    @Value("${aliyun.loginMessageTemplate}")
    private String LoginMessageTemplate;

    @Value("${aliyun.registerMessageTemplate}")
    private String RegisterMessageTemplate;

    /**
     *
     * @param param
     * @param phone
     * @param type
     * @return
     */
    @Override
    public boolean sendVerify(Map<String, Object> param, String phone, int type) {
        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        DefaultProfile profile =
                DefaultProfile.getProfile(
                        "default",
                        AccessKey,
                        AccessSecret);
            // 地域ID, 账号的AccessKey ID, 账号AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定的参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com"); //设置跨域域名domain(该产品的服务地址)
        request.setVersion("2017-05-25");           //设置版本(该API的版本号，格式为YYYY-MM-DD。)
        request.setAction("SendSms");               //设置要调用的接口名称，RPC风格需要获取Action参数来指定api名称
        //设置发送相关的参数
        request.putQueryParameter("PhoneNumbers",phone); //手机号
        request.putQueryParameter("SignName",SignName); //签名名称
        if(type == 1) {
            request.putQueryParameter("TemplateCode",ChangeMessageTemplate); //模板code 变更信息验证码
        } else if(type == 2) {
            request.putQueryParameter("TemplateCode",LoginMessageTemplate); //模板code 登陆验证码
        } else {
            request.putQueryParameter("TemplateCode",RegisterMessageTemplate); //模板code 注册验证码
        }
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递

        try {
            //最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
